#This script executes event-study regressions to estimate cohort-specific 
#associations between first-generation prenatal exposure to nonattainment 
#and additional second-generation outcomes.

rm(list=ls())
gc()
library(reldist)
library(ineq)
library(stargazer)
library(data.table)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(stringdist)
library(lfe)
library(haven)
library(readr)
library(broom)
library(tidylog)
library(rdrobust)

# Set the working directory
setwd("/projects/opp_env/caa1970/")

# Source external R scripts for custom functions
source("Code/Child_Outcomes/child_outcomes_functions.R")
source("Code/rounding.r")

# Load the second-generation analysis dataset
load("Data/second_gen_college_analysis_data_jpemicro.rda")

# Load the second-generation earnings analysis dataset
load("Data/second_gen_earnings_23_plus_analysis_data_jpemicro.rda")

# Event Studies

## Fig A2a - Second-Gen High School Diploma

eventA2a <- felm(HS_grad ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,                
               data=regdata%>% filter(year%in%1969:1975, !is.na(all_fullterm), !is.na(parent_race), !is.na(PI_PC))) 
summary(eventA2a)

#Save the underlying points
figA2a <- eventA2a %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figA2a, file="JPE_Micro/Output/A2_Figures/FigA2a.csv", row.names=F)

## Fig A2b - Second-Gen log Real Earnings

eventA2b <- felm(log_real_wages ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,                
               data=regdata%>% filter(year%in%1969:1975, real_wages>0, real_wages < wage_cap, !is.na(all_fullterm),!is.na(parent_race), !is.na(PI_PC))) 
summary(eventA2b)

#Save the underlying points
figA2b <- eventA2b %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figA2b, file="JPE_Micro/Output/A2_Figures/FigA2b.csv", row.names=F)

## Fig A2c - Second-Gen Positive Real Earnings

eventA2c <- felm(real_wages ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,                
               data=regdata%>% filter(year%in%1969:1975, real_wages> 0, real_wages < wage_cap, !is.na(all_fullterm),!is.na(parent_race), !is.na(PI_PC))) 
summary(eventA2c)

#Save the underlying points
figA2c <- eventA2c %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figA2c, file="JPE_Micro/Output/A2_Figures/FigA2c.csv", row.names=F)

## Fig A2d - Second-Gen Real Earnings

eventA2d <- felm(real_wages ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor,                
               data=regdata%>% filter(year%in%1969:1975, real_wages < wage_cap, !is.na(all_fullterm),!is.na(parent_race), !is.na(PI_PC))) 
summary(eventA2d)

#Save the underlying points
figA2d <- eventA2d %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figA2d, file="JPE_Micro/Output/A2_Figures/FigA2d.csv", row.names=F)
